package com.ambow.store.service.impl;

import com.ambow.store.entity.Address;
import com.ambow.store.entity.OrderItem;
import com.ambow.store.mapper.OrderItemMapper;
import com.ambow.store.service.AddressService;
import com.ambow.store.service.CartService;
import com.ambow.store.service.OrderItemService;
import com.ambow.store.service.exception.InsertException;
import com.ambow.store.vo.CartVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ambow.store.entity.Order;
import com.ambow.store.service.OrderService;
import com.ambow.store.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author MyComputer
* @description 针对表【t_order】的数据库操作Service实现
* @createDate 2022-11-13 14:16:29
*/
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService{

    @Autowired
    private AddressService addressService;

    @Autowired
    private CartService cartService;

    @Autowired
    private OrderItemService orderItemService;

    @Override
    public Order create(Integer aid, Integer[] cids, Integer uid, String username) {
        //创建当前时间对象
        Date date = new Date();

        //根据cids查询所有勾选的购物车列表中的数据
        List<CartVO> carts = cartService.getVOByCid(uid, cids);

        //计算这些商品的总价
        long totalPrice = 0;
        for (CartVO cart : carts) {
            totalPrice += cart.getRealPrice() + cart.getNum();
        }

        //创建订单对象
        Order order = new Order();
        //补全数据：uid
        order.setUid(uid);
        //查询收获地址
        Address address = addressService.getByAid(aid, uid);
        //补全数据，售后地址相关的6项
        order.setRecvName(address.getName());
        order.setRecvPhone(address.getPhone());
        order.setRecvProvince(address.getProvinceName());
        order.setRecvCity(address.getCityName());
        order.setRecvArea(address.getAreaName());
        order.setRecvAddress(address.getAddress());
        //补全数据：totalPrice
        // 补全数据：totalPrice
        order.setTotalPrice(totalPrice);
        // 补全数据：status
        order.setStatus(0);
        // 补全数据：下单时间
        order.setOrderTime(date);
        // 补全数据：日志
        order.setCreatedUser(username);
        order.setCreatedTime(date);
        order.setModifiedUser(username);
        order.setModifiedTime(date);
        //插入订单数据
        boolean save = this.save(order);
        if ( !save){
            throw new InsertException("插入订单数据时出现未知错误，请联系系统管理员");
        }


        //遍历carts，循环插入订单商品数据
        for (CartVO cart : carts) {
            //创建订单商品数据
            OrderItem item = new OrderItem();
            // 补全数据：setOid(order.getOid())
            item.setOid(order.getOid());
            // 补全数据：pid, title, image, price, num
            item.setPid(cart.getPid());
            item.setTitle(cart.getTitle());
            item.setImage(cart.getImage());
            item.setPrice(cart.getRealPrice());
            item.setNum(cart.getNum());
            // 补全数据：4项日志
            item.setCreatedUser(username);
            item.setCreatedTime(date);
            item.setModifiedUser(username);
            item.setModifiedTime(date);
            //插入订单商品数据
            boolean save1 = orderItemService.save(item);
            if ( !save1){
                throw new InsertException("插入商品订单数据时出现未知错误，请联系系统管理员");
            }
        }
        //返回
        return order;
    }
}




